package cn.seaboot.admin.user.web;

import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.admin.user.bean.Role;
import cn.seaboot.admin.user.context.UserContext;
import cn.seaboot.admin.user.service.RoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 系统角色 [t_sys_role] - Controller
 * <p>
 *
 * @author Mr.css on 2018-6-20
 */
@Controller
@RequestMapping("sys/role")
@Tag(name = "系统角色 [t_sys_role] 管理")
public class RoleCtrl {

    @Resource
    private RoleService roleService;

    @Resource
    private UserContext userContext;

    /**
     * 增
     *
     * @param role pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "系统角色 [t_sys_role] 增")
    @RequestMapping(value = "info", method = RequestMethod.POST)
    public int saveInfo(@Validated Role role) {
        return roleService.insert(role);
    }

    /**
     * 删
     *
     * @param id id
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "系统角色 [t_sys_role] 删")
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    public int deleteInfo(@Params String id) {
        return roleService.deleteById(id);
    }

    /**
     * 查
     *
     * @param id id
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "系统角色 [t_sys_role] 查")
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public Role getInfo(@Params String id) {
        return roleService.queryById(userContext.getOrgId(), id);
    }

    /**
     * 改
     *
     * @param role pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "系统角色 [t_sys_role] 改")
    @RequestMapping(value = "info", method = RequestMethod.PUT)
    public int updateInfo(@Params String id, @Validated Role role) {
        return roleService.updateRole(role.getId(), role);
    }

    /**
     * 查列表
     *
     * @return list
     */
    @ResponseBody
    @Operation(description = "系统角色 [t_sys_role] 查询全部角色")
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<Role> queryPage(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        params.put("ordId", userContext.getOrgId());
        return Pagination.apply(params, map-> roleService.queryList(map));
    }

    /**
     * 查列表
     *
     * @return list
     */
    @ResponseBody
    @Operation(description = "系统角色 [t_sys_role] 查询全部角色")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public List<Role> queryList(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        params.put("ordId", userContext.getOrgId());
        return roleService.queryList(params);
    }

    /**
     * 查列表
     *
     * @return list
     */
    @ResponseBody
    @Operation(description = "系统角色 [t_sys_role] 查询可编辑的角色")
    @RequestMapping(value = "grant", method = RequestMethod.GET)
    public List<Role> grantList( @RequestParam Map<String, Object> params) {
        return roleService.queryList(params);
    }
}
